﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GFluentCode.Tools
{
    /// <summary>
    /// 键值对对象
    /// </summary>
    [Serializable]
    public class GPair<Tvalue>
    {
        /// <summary>
        /// 键
        /// </summary>
        public virtual string Name { get; set; }
        /// <summary>
        /// 值
        /// </summary>
        public virtual Tvalue Value { get; set; }

        /// <summary>
        /// 实例化键值对
        /// </summary>
        public GPair() : this("", default(Tvalue)) { }
        /// <summary>
        /// 实例化键值对
        /// </summary>
        /// <param name="name"></param>
        public GPair(string name) : this(name, default(Tvalue)) { }
        /// <summary>
        /// 实例化键值对
        /// </summary>
        /// <param name="name">键</param>
        /// <param name="value">值</param>
        public GPair(string name,Tvalue value)
        {
            Name = name;
            Value = value;
        }
        /// <summary>
        /// 实例化键值对
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public GPair(string name,object value)
        {
            Name = name;

            if (value == null)
            {
                Value = default(Tvalue);
                return;
            }

            Tvalue v;
            try
            {
                v = (Tvalue)value;
                Value = v;
            }
            catch
            {
                throw new Exception("值不能转换为类型："+typeof(Tvalue).FullName);
            }
            
        }
    }
}
